package com.baitai.web.controller.system;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baitai.common.utils.SecurityUtils;
import com.baitai.system.domain.vo.Express100DTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baitai.common.annotation.Log;
import com.baitai.common.core.controller.BaseController;
import com.baitai.common.core.domain.AjaxResult;
import com.baitai.common.enums.BusinessType;
import com.baitai.system.domain.ProductOrders;
import com.baitai.system.service.IProductOrdersService;
import com.baitai.common.utils.poi.ExcelUtil;
import com.baitai.common.core.page.TableDataInfo;

/**
 * 商品订单Controller
 * 
 * @author xx
 * @date 2024-07-21
 */
@RestController
@RequestMapping("/system/productOrders")
public class ProductOrdersController extends BaseController
{
    @Autowired
    private IProductOrdersService productOrdersService;

    /**
     * 查询商品订单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ProductOrders productOrders)
    {
        startPage();
        List<ProductOrders> list = productOrdersService.selectProductOrdersList(productOrders);
        return getDataTable(list);
    }

    /**
     * 导出商品订单列表
     */
    @Log(title = "商品订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductOrders productOrders)
    {
        List<ProductOrders> list = productOrdersService.selectProductOrdersList(productOrders);
        ExcelUtil<ProductOrders> util = new ExcelUtil<ProductOrders>(ProductOrders.class);
        util.exportExcel(response, list, "商品订单数据");
    }

    /**
     * 获取商品订单详细信息
     */
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return success(productOrdersService.selectProductOrdersByOrderId(orderId));
    }

    /**
     * 新增商品订单
     */
    @Log(title = "商品订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductOrders productOrders)
    {
        return toAjax(productOrdersService.insertProductOrders(productOrders));
    }

    /**
     * 修改商品订单
     */
    @Log(title = "商品订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductOrders productOrders)
    {
        return toAjax(productOrdersService.updateProductOrders(productOrders));
    }

    /**
     * 删除商品订单
     */
    @Log(title = "商品订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(productOrdersService.deleteProductOrdersByOrderIds(orderIds));
    }

    @PostMapping("/appList")
    public AjaxResult appList(@RequestBody ProductOrders productOrders)
    {
        startPage();
        productOrders.setUserId(SecurityUtils.getUserId());
        List<ProductOrders> list = productOrdersService.appList(productOrders);
        return AjaxResult.success(getDataTable(list));
    }

    @GetMapping("/findOrder")
    public AjaxResult findOrder(Long orderId)
    {
         productOrdersService.findOrder(orderId);
        return AjaxResult.success();
    }
    @PostMapping("/callback/{orderId}")
    public AjaxResult callback(@PathVariable Long orderId, @RequestParam Map request)
    {
        System.out.println(JSON.toJSONString(request));
        productOrdersService.callback(orderId, JSONObject.parseObject(JSON.toJSONString(request)));
        return AjaxResult.success();
    }
}
